package bitmap;

public class Color
{
	int red = 0;
	int green = 0;
	int blue = 0;
	double gamma = 1;
	


	public final static Color WHITE= new Color(255,255,255);
	public final static Color BLACK= new Color(0,0,0);
	public final static Color RED= new Color(255,0,0);
	public final static Color GREEN= new Color(0,255,0);
	public final static Color BLU= new Color(0,0,255);
	public final static Color YELLOW= new Color(255,255,0);
	public final static Color CYAN= new Color(0,255,255);
	public final static Color MAGENTA= new Color(255,0,255);
	public final static Color GREY= new Color(128,128,128);

	public Color(int rgbInt)
	{
		this.blue = rgbInt / (1 << 16);
		this.green = (rgbInt - (blue << 16)) / (1 << 8);
		this.red = (rgbInt - (blue << 16) - (green << 8));
		this.gamma = 1.;
		normalizeColor();
	}

	public Color(int red, int green, int blue, double gamma)
	{
		this.red = red;
		this.green = green;
		this.blue = blue;
		this.gamma = gamma;
		normalizeColor();
	}

	private void normalizeColor()
	{
		if(red==255 && green==255 && blue==255 )
			return;
		if(red>255 && green>255 && blue>255 ){
			red=red-255;
			blue=blue-255;
			green=green-255;
			normalizeColor();
		}
		
		if(red>255)
			red=255;
		if(green>255)
			green=255;
		if(blue>255)
			blue=255;

		if(red<0)
			red=0;
		if(green<0)
			green=0;
		if(blue<0)
			blue=0;

		if(gamma>1.)
			gamma=1.;
		if(gamma<0)
			gamma=0.;
		
	}

	public Color(int red, int green, int blue)
	{
		this.red = red;
		this.green = green;
		this.blue = blue;
		normalizeColor();
	}

	public int getRgbInt()
	{
		return (int)(red*gamma) | (int)(green*gamma) << 8 | (int)(blue*gamma) << 16;
	}

	public static Color addColors(Color a, Color b)
	{
		
		int red = (int) (a.getRedGamma()+b.getRedGamma());
		int green = (int) (a.getGreenGamma()+ b.getGreenGamma());
		int blue = (int) (a.getBlueGamma()+ b.getBlueGamma());
		double gamma = Math.max(a.getGamma(), b.getGamma());
		Color res =  new Color(red, green, blue, gamma);
		return res;
	}

	public int getRed()
	{
		return red;
	}

	public int getRedGamma()
	{
		return (int) (red * gamma);
	}

	public void setRed(int red)
	{
		this.red = red;
	}

	public int getGreen()
	{
		return green;
	}

	public int getGreenGamma()
	{
		return (int) (green * gamma);
	}

	public void setGreen(int green)
	{
		this.green = green;
	}

	public int getBlue()
	{
		return blue;
	}

	public int getBlueGamma()
	{
		return (int) (blue * gamma);
	}

	public void setBlue(int blue)
	{
		this.blue = blue;
	}

	public double getGamma()
	{
		return gamma;
	}

	public void setGamma(double gamma)
	{
		this.gamma = gamma;
	}

	@Override
	public String toString()
	{
		String res="r:"+red+" g:"+green+" b:"+blue+" val:"+getRgbInt();
		return res;
	}
}
